package cn.jgy.dynamic.thread.pool.sdk.registry.Redis;


import cn.jgy.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import cn.jgy.dynamic.thread.pool.sdk.domain.model.valobj.RegistryEnumVO;
import cn.jgy.dynamic.thread.pool.sdk.registry.IRegistry;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;

import java.time.Duration;
import java.util.List;

/**
 * @Author: 蒋光耀
 *@Version: 1.0
 *@description: Redis只是其中一种注册方式，当然也可以用ZP,Mysql
 * @Date:  2024-05-18 12:14
 */


public class RedisRegistry implements IRegistry {

    private final RedissonClient redissonClient;

    public RedisRegistry(RedissonClient redissonClient) {
        this.redissonClient=redissonClient;
    }

    @Override
    public void reportThreadPoolList(List<ThreadPoolConfigEntity> threadPoolConfigEntities) {
       RList<ThreadPoolConfigEntity> rList= redissonClient.getList(RegistryEnumVO.THREAD_POOL_CONFIG_LIST_KEY.getKey());
       //将列表的线程池添加进来
       rList.addAll(threadPoolConfigEntities);

    }

    @Override
    public void reportThreadPoolOne(ThreadPoolConfigEntity threadPoolConfigEntity) {
        String cacheKey=RegistryEnumVO.THREAD_POOL_CONFIG_PARAMETER_LIST_KEY.getKey()+"_"+threadPoolConfigEntity.getAppName() + "_" + threadPoolConfigEntity.getThreadPoolName();
        RBucket<ThreadPoolConfigEntity> bucket=redissonClient.getBucket(cacheKey);
        //对保存的key通过设置过期时间进行存储
        bucket.set(threadPoolConfigEntity, Duration.ofDays(15));

    }
}
